package com.enterprise.support.utility.generator.menu;

import java.io.Serializable;
import java.util.LinkedList;
import java.util.List;

public class MenuNode implements Serializable {

    private Long nodeId;
    private Long fatherNodeId;

    private String name;
    private String panelName;
    private String constructor;

    private MenuNode parent;
    public List<MenuNode> children = new LinkedList<MenuNode>();


    public MenuNode() {
        this(null, null);
    }

    public MenuNode(MenuNode parent, String name) {
        this(parent, name, null);
    }

    public MenuNode(MenuNode parent, String name, String panelName) {
        this(parent, name, panelName, null);
    }

    public MenuNode(MenuNode parent, String name, String panelName, String constructor) {
        this.parent = parent;//这个节点的父节点就是传进来的父节点
        if (parent != null)
            parent.children.add(this);//如果父节点不是空，就让父节点加上这个对象，这个对象就是子节点
        this.name = name;
        this.panelName = panelName;
        this.constructor = constructor;
    }

    public boolean containsOrEqualsChildren(List<MenuNode> roleNodes) {
        if (contains(roleNodes, this)) {
            return true;
        }
        return false;
    }

    private boolean contains(List<MenuNode> roleNodes, MenuNode node) {
        if (roleNodes.contains(node))
            return true;
        for (MenuNode child : node.children)
            return contains(roleNodes, child);
        return false;
    }

    public List<MenuNode> getChildren() {
        return children;
    }

    public void addChild(MenuNode child) {
        if (!children.contains(child))
            children.add(child);
    }

    public void removeChild(MenuNode child) {
        if (children.contains(child))
            children.remove(child);
    }

    public boolean hasChildren() {
        return children.size() > 0;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public MenuNode getParent() {
        return parent;
    }

    public void setParent(MenuNode parent) {
        this.parent = parent;
    }

    public String getConstructor() {
        return constructor;
    }

    public void setConstructor(String constructor) {
        this.constructor = constructor;
    }

    public String getPanelName() {
        return panelName;
    }

    public void setPanelName(String panelName) {
        this.panelName = panelName;
    }

    public Long getFatherNodeId() {
        return fatherNodeId;
    }

    public void setFatherNodeId(Long fatherNodeId) {
        this.fatherNodeId = fatherNodeId;
    }

    public Long getNodeId() {
        return nodeId;
    }

    public void setNodeId(Long nodeId) {
        this.nodeId = nodeId;
        for (MenuNode child : children)
            child.setFatherNodeId(nodeId);
    }

    @Override
    public String toString() {
        return name + ":" + nodeId + ":" + fatherNodeId;
    }

    @Override
    public boolean equals(Object obj) {
        if (obj != null && obj instanceof MenuNode) {
            MenuNode node = (MenuNode) obj;
            try {
                return node.nodeId.equals(nodeId);
            } catch (NullPointerException e) {

            }
        }
        return false;
    }

}
